import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Date;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;


public class Indexer {
	public static void main(String[] args) throws IOException {
		File indexDir = new File(args[0]);
		File dataDir = new File(args[1]);
		
		long start = new Date().getTime();
		int numIndexed= index(indexDir, dataDir);
		long end = new Date().getTime();
		System.out.println("Indexing " + numIndexed + " files too" + (end - start) + " miliseconds");
		
	}

	private static int index(File indexDir, File dataDir) throws IOException {
		IndexWriter writer = new IndexWriter(indexDir, new StandardAnalyzer(), true);
		writer.setUseCompoundFile(false);
		
		indexDirectory(writer, dataDir);
		
		int numIndexed = writer.docCount();
		writer.optimize();
		writer.close();
		return numIndexed;
	}

	private static void indexDirectory(IndexWriter writer, File dataDir) throws IOException {
		File[] files = dataDir.listFiles();
		for (int i = 0; i < files.length; i++) {
			File f = files[i];
			if (f.isDirectory()){
				indexDirectory(writer, f);
			} else if (f.getName().endsWith(".html")){
				indexFile(writer, f);
			}
		}
		
	}

	private static void indexFile(IndexWriter writer, File f) throws IOException {
		System.out.println("Indexing " + f.getCanonicalPath());
		Document doc = new Document();
		doc.add(Field.Text("contents", new FileReader(f)));
		doc.add(Field.Text("filename", f.getCanonicalPath()));
		writer.addDocument(doc);
	}
}
